package com.manage.utils;


import net.coobird.thumbnailator.Thumbnails;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

/**
 * @author Fitch
 * 2019/10/23
 */
public class test {


    /**
     * 功能：获取图片像素
     * * @param filePath 图片路径
     */
    public static void getPixel(String filePath){
        File file = new File(filePath);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int width = bi.getWidth(); // 像素
        int height = bi.getHeight(); // 像素
        System.out.println("width=" + width + ",height=" + height + ".");
    }


    public static void resizeImage(String srcPath, String desPath,int width1,int height1) throws IOException {

        File srcFile = new File(srcPath);
        Image srcImg = ImageIO.read(srcFile);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(srcFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int width = bi.getWidth(); // 像素
        int height = bi.getHeight(); // 像素
        BufferedImage buffImg = null;
        buffImg = new BufferedImage(width1, height1, BufferedImage.TYPE_INT_ARGB);
        //使用TYPE_INT_RGB修改的图片会变色
        buffImg.getGraphics().drawImage(
                srcImg.getScaledInstance(width1, height1, Image.SCALE_DEFAULT), 0,
                0, null);

        ImageIO.write(buffImg, "PNG", new File(desPath));
    }


    public static void main(String []args) throws IOException{
        //getPixel("C:\\Users\\Fitch\\Desktop\\临时\\aa.png");
        resizeImage("C:\\\\Users\\\\Fitch\\\\Desktop\\\\临时\\\\bb.png","C:\\\\Users\\\\Fitch\\\\Desktop\\\\临时\\\\bb1.png",794,1123);
       // scale("C:\\Users\\Fitch\\Desktop\\临时\\aa.png","C:\\Users\\Fitch\\Desktop\\临时\\aa1.png");
    }
    private static boolean dealPictureLocal(String sourceUrl, String objectUrl, float scale) {
        File file = new File(sourceUrl);
        File[] files = file.listFiles();
        for (File f : files) {
            File temp = new File(f.toString());
            if (temp.isDirectory()) {
                dealPictureLocal(f.toString(), objectUrl + StrUtil.split(f.toString(), sourceUrl)[1], scale);
            } else {
                if (f.toString().indexOf(".jpg") > 0 || f.toString().indexOf(".png") > 0 || f.toString().indexOf(".jpeg") > 0 || f.toString().indexOf(".bmp") > 0) {
                    OutputStream os = null;
                    try {
                        String sourceStr = f.toString();
                        String fileName = StrUtil.split(f.toString(), sourceUrl)[1];
                        String oldFileName = sourceStr;
                        String objectStr = objectUrl + fileName;
                        File objectDir = new File(objectUrl);
                        if (!objectDir.exists()) {
                            objectDir.mkdirs();
                        }
                        Thumbnails.of(sourceStr).scale(scale).toFile(objectStr);
                        System.out.println(Thread.currentThread().getName() + ":===" + f.toString());
                        if (!oldFileName.equals(objectStr)) {
                            File file1 = new File(oldFileName);
                            if (!file1.exists()) {
                                System.out.println("删除文件失败:" + fileName + "不存在！");
                            } else {
                                if (file1.isFile()) {
                                    file1.delete();
                                }
                            }
                        }
                    } catch (Exception e) {
                        return false;
                    }
                }
            }
        }
        return true;

    }
}
